// pages/shopping/shopping.js
var util = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hiddenName: false,
    hidden: true,

    totalPrice: 0.00, // 总价
    selectAllStatus: false, // 全选状态，默认全选
  },
  getTotalPrice() {
    let carts = this.data.arraa;
    let total = 0;
    for (let i = 0; i < carts.length; i++) {
      if (carts[i].selected) { // 判断选中才会计算价格
        total += carts[i].number * carts[i].goodsprice;
      }
    }
    this.setData({
      arraa: carts,
      totalPrice: total.toFixed(2),
    });
  },
  selectList(e) {
    const index = e.currentTarget.dataset.index;
    let carts = this.data.arraa; // 获取购物车列表
    const selected = carts[index].selected; // 获取当前商品的选中状态
    carts[index].selected = !selected; // 改变状态

    this.setData({
      arraa: carts,
    });
    this.getTotalPrice();
  },
  selectAll(e) {
    let selectAllStatus = this.data.selectAllStatus; // 是否全选状态
    selectAllStatus = !selectAllStatus;
    let carts = this.data.arraa;

    for (let i = 0; i < carts.length; i++) {
      carts[i].selected = selectAllStatus; // 改变所有商品状态
    }
    this.setData({
      selectAllStatus: selectAllStatus,
      arraa: carts
    });
    this.getTotalPrice();
  },

  goodsinfo(e) {
    var id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/news-info/news-info?id=' + id + '',
    })
  },
  result() {
    let name = getApp().globalData.usersname
    var time = util.formatTime(new Date());
    console.log(name, time)
    var num = Math.floor(Math.random() * 100000000000000 + 100000000000000)
    console.log(num)
    var that = this;
    let carts = this.data.arraa;
    for (let i = 0; i < carts.length; i++) {
      if (carts[i].selected) {
        var id = carts[i].id
        var goodsname = carts[i].goodsname
        var goodsimg = carts[i].goodsimg
        var goodsnews = carts[i].goodsnews
        var number = carts[i].number
        var goodsprice = carts[i].goodsprice
        var goodsshop = carts[i].goodsshop
        var type = carts[i].type
        var goodsid = carts[i].goodsid
        var numnum = carts[i].num
        // console.log(goodsname, goodsimg, goodsnews, number, goodsprice, goodsshop, type, goodsid)
        // console.log(numnum)
        wx.request({
          url: 'http://localhost/shopping/goods_num_add.php',
          header: {
            'Content-Type': 'application/json'
          },
          data: {
            id: goodsid,
            num: numnum,
            number: number,
          },
          success: function (res) {
            console.log(res.data)

          },
          fail: function (res) {
            console.log("fail");
          }
        })
        wx.request({
          url: 'http://localhost/shopping/admin_order_add.php',
          header: {
            'Content-Type': 'application/json'
          },
          data: {
            name: name,
            startdate: time,
            ordernumber: num,
            goodsname: goodsname,
            goodsimg: goodsimg,
            goodsnews: goodsnews,
            goodsnumber: number,
            goodsprice: goodsprice,
            goodsshop: goodsshop,
            type: type,
            goodsid: goodsid,
            num: numnum
          },
          success: function (res) {
            // console.log(res.data)
            wx.showToast({
              title: '生成订单成功',
              icon: 'success',
              duration: 1000
            })
            setTimeout(function () {
              wx.showModal({
                title: '订单已生成',
                content: '是否跳转到订单页面',
                success(res) {
                  if (res.confirm) {
                    console.log('用户点击确定')
                    wx.navigateTo({
                      url: '/pages/order-pay/order-pay',
                    })
                  } else if (res.cancel) {
                    console.log('用户点击取消')
                  }
                }
              })
            }, 1500)

            wx.request({
              url: 'http://localhost/shopping/shopping_del.php',
              header: {
                'Content-Type': 'application/json'
              },
              data: {
                id: id,
              },
              success: function (res) {
                // console.log(res.data)
                console.log('商品删除成功')
                that.setData({})
              },
              fail: function (res) {
                console.log("fail");
              }
            })
            that.setData({})
          },
          fail: function (res) {
            console.log("fail");
          }
        })
      }
    }
  },
  login() {
    wx.showModal({
      title: '提示',
      content: '是否进行用户登录',
      success: function (res) {
        if (res.confirm) { //这里是点击了确定以后
          console.log('登录')
          wx.navigateTo({
            url: '/pages/login/login',
          })
        } else { //这里是点击了取消以后
          console.log('取消')
        }
      }
    })
  },
  del(e) {
    var id = e.currentTarget.dataset.id
    var that = this;
    wx.showModal({
      title: '',
      content: '是否确认删除',
      success(res) {
        if (res.confirm) {
          console.log('用户点击确定')

          wx.request({
            url: 'http://localhost/shopping/shopping_del.php',
            header: {
              'Content-Type': 'application/json'
            },
            data: {
              id: id,
            },
            success: function (res) {
              // console.log(res.data)
              console.log('商品删除成功')
              that.User();
              that.setData({

              })
            },
            fail: function (res) {
              console.log("fail");
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })

  },
  addnumber(e) {
    var id = e.currentTarget.dataset.id
    var number = e.currentTarget.dataset.number
    var that = this;
    wx.request({
      url: 'http://localhost/shopping/shopping_number_add.php',
      header: {
        'Content-Type': 'application/json'
      },
      data: {
        id: id,
        number: number,
      },
      success: function (res) {
        // console.log(res.data)
        console.log('数量增加成功')
        that.setData({})
      },
      fail: function (res) {
        console.log("fail");
      }
    })
    that.User();

  },
  delnumber(e) {
    var id = e.currentTarget.dataset.id
    var number = e.currentTarget.dataset.number
    var num = e.currentTarget.dataset.num
    var goodsid = e.currentTarget.dataset.goodsid
    var that = this;
    if (number == 1) {
      console.log('是否删除商品')
      wx.showModal({
        title: '删除商品',
        content: '是否删除商品?',
        success: function (res) {
          if (res.confirm) { //这里是点击了确定以后
            console.log('删除')
            wx.request({
              url: 'http://localhost/shopping/shopping_del.php',
              header: {
                'Content-Type': 'application/json'
              },
              data: {
                id: id,
              },
              success: function (res) {
                // console.log(res.data)
                console.log('商品删除成功')
                that.setData({})
                that.User();
              },
              fail: function (res) {
                console.log("fail");
              }
            })
          } else { //这里是点击了取消以后
            console.log('取消')
          }
        }
      })
    } else {
      var that = this
      wx.request({
        url: 'http://localhost/shopping/shopping_number_del.php',
        header: {
          'Content-Type': 'application/json'
        },
        data: {
          id: id,
          number: number,
        },
        success: function (res) {
          console.log('数量减少成功')
          // console.log(res.data)
        },
        fail: function (res) {
          console.log("fail");
        }
      })
      that.User();
    }

  },

  User: function () {
    let name = getApp().globalData.usersname
    var that = this;
    wx.request({
      url: 'http://localhost/shopping/users_shopping.php',
      header: {
        'Content-Type': 'application/json'
      },
      data: {
        name: name,
      },
      success: function (res) {
        console.log(res.data)
        that.setData({
          arraa: res.data.shopping,
        })
      },
      fail: function (res) {
        console.log("fail");
      }
    })

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let name = getApp().globalData.usersname
    var that = this
    if (!name) {
      console.log('您还未登录')
    } else {
      that.User();
      that.setData({
        hiddenName: !that.data.hiddenName,
        hidden: !that.data.hidden
      })
    }

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    let name = getApp().globalData.usersname
    var that = this
    if (!name) {
      console.log('您还未登录')
    } else {
      that.User();
      that.setData({
        hiddenName: !that.data.hiddenName,
        hidden: !that.data.hidden
      })

    }
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})